package com.cj.framework.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.yuanduan.mplus.R;

/**
 * Created by cj on 2016/6/15.
 * Email:781806481@qq.com
 */
public class HorizontalBarChart extends View {
    private int backgroundColor;
    private int progressColor;
    private int maxLength;
    private int warningLength;
    private int specifyLength;
    private int currentLength=0;

    private Paint borderPaint;
    private Paint contentPaint;
    private Paint triangularPaint;
    private Paint textPaint;
    private boolean isDraw;

    public HorizontalBarChart(Context context) {
        this(context,null);
    }

    public HorizontalBarChart(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public HorizontalBarChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //获取xml文件自定义值
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.HorizontalBarChart);
        backgroundColor=typedArray.getColor(R.styleable.HorizontalBarChart_backgroundColor, Color.GRAY);
        progressColor=typedArray.getColor(R.styleable.HorizontalBarChart_progressColor,Color.BLUE);
        maxLength=typedArray.getInt(R.styleable.HorizontalBarChart_maxLength,100);
        specifyLength=typedArray.getInt(R.styleable.HorizontalBarChart_specifyLength,0);
        warningLength=typedArray.getInt(R.styleable.HorizontalBarChart_warningLength,0);
        typedArray.recycle();
        init();
    }

    private void init() {
        Log.d("xx",specifyLength+","+warningLength+","+maxLength);
        borderPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        borderPaint.setColor(backgroundColor);
        contentPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        contentPaint.setColor(progressColor);
        triangularPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        triangularPaint.setColor(Color.GRAY);
        textPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(progressColor);
        textPaint.setTextSize(30);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width=getWidth()-getPaddingLeft()-getPaddingRight();
        int height=getHeight()-getPaddingTop()-getPaddingBottom();

        //画边框
        RectF borderRect=new RectF(0,0,width,height/2.0f);
        canvas.drawRect(borderRect,borderPaint);

        //画文字
        float textWidth=textPaint.measureText(specifyLength+"/"+maxLength);
        int textX=(int)(width-textWidth);
        //-textPaint.descent()-textPaint.ascent()是baseline到文字顶部的长度；
        //textPaint.ascent()是负数，所以需要添加负号
        int textY= (int) (height/2.0f-(textPaint.descent()+textPaint.ascent())+5);
        canvas.drawText(specifyLength+"/"+maxLength,textX,textY,textPaint);

        if(!isDraw){
            return;
        }
        //画内容
        RectF contentRect=new RectF(0,0,currentLength*1.0f/maxLength*width,height/2.0f);
        canvas.drawRect(contentRect,contentPaint);

        //画三角形
        if(currentLength>=specifyLength){
            Path path=new Path();
            path.moveTo(warningLength*1.0f/maxLength*width,height/2.0f);
            path.lineTo(warningLength*1.0f/maxLength*width+5* Resources.getSystem().getDisplayMetrics().density,height/2.0f+5* Resources.getSystem().getDisplayMetrics().density);
            path.lineTo(warningLength*1.0f/maxLength*width-5* Resources.getSystem().getDisplayMetrics().density,height/2.0f+5* Resources.getSystem().getDisplayMetrics().density);
            path.lineTo(warningLength*1.0f/maxLength*width,height/2.0f);
            canvas.drawPath(path,triangularPaint);

        }
    }

    private Handler handler=new Handler();
    public void animal(){
        isDraw=true;
        currentLength=0;
        invalidate();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(currentLength<specifyLength){
                    currentLength+=5;
                    invalidate();
                    handler.postDelayed(this,50);
                }else {
                    isDraw=false;
                }
            }
        },500);
    }
    public void animal(final int timespan, final int distance){
        isDraw=true;
        currentLength=0;
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(currentLength<warningLength){
                    currentLength+=distance;
                    invalidate();
                    handler.postDelayed(this,timespan);
                }else{
                    isDraw=false;
                }
            }
        },500);
    }

    public void setLength(int specifyLength,int warningLength,int maxLength){
        this.specifyLength=specifyLength;
        this.warningLength=warningLength;
        this.maxLength=maxLength;
        animal();
    }
}
